package com.chris.homecare.screens
{
	import com.chris.homecare.components.BreadcrumbComponenet;
	import com.chris.homecare.components.HeaderComponent;
	import com.chris.homecare.components.StarRatting;
	import com.chris.homecare.data.Employee;
	import com.chris.homecare.data.FetchRemoteData;
	import com.chris.homecare.utils.CommonUtils;
	import com.chris.homecare.utils.ScreenNames;
	
	import flash.filesystem.File;
	
	import feathers.controls.Button;
	import feathers.controls.ImageLoader;
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.controls.Screen;
	import feathers.layout.AnchorLayout;
	import feathers.layout.AnchorLayoutData;
	import feathers.layout.HorizontalLayout;
	import feathers.layout.VerticalLayout;
	import feathers.layout.VerticalLayoutData;
	
	import starling.events.Event;
	
	[Event(name="triggerPerformance",type="starling.events.Event")]
	
	public class EmployeeScreen extends Screen
	{
		
		public static const TRIGGER_PERFORMANCE:String = "triggerPerformance";
		
		private var header:HeaderComponent;
		private var breadcrumbComp:BreadcrumbComponenet;
		
		private var employeeName:Label;
		private var employeeTitle:Label;
		private var employeeImage:ImageLoader;
		private var imageFrame:ImageLoader;
		private var starRattingComp:StarRatting;
		private var ratingLabel:Label;
		private var reviewBtn:Button;
		private var hireButton:Button;
		private var performanceButton:Button;

		private var layoutGroup:LayoutGroup;
		private var buttonGroup:LayoutGroup;
		private var anchorLayoutGroup:LayoutGroup;

		private var employeeImageData:AnchorLayoutData;
		private var empTitleLblData:AnchorLayoutData;
		private var starRatingCompData:AnchorLayoutData;
		private var employeeData:Employee;

		public function EmployeeScreen()
		{
			super();
		}
		
		override protected function initialize():void
		{
			CommonUtils.previousScreen = ScreenNames.TEAM_SCREEN;
			
			employeeData = FetchRemoteData.choosenEmployeeData;
			
			var layout:VerticalLayout = new VerticalLayout();
			layout.horizontalAlign = HorizontalLayout.HORIZONTAL_ALIGN_CENTER;
			this.layout = layout;
			
			header = new HeaderComponent();
			addChild(header);
			
			breadcrumbComp = new BreadcrumbComponenet("Info", "Team");
			addChild(breadcrumbComp);

			anchorLayoutGroup = new LayoutGroup();
			var anchorLayout:AnchorLayout = new AnchorLayout();
			anchorLayoutGroup.layout = anchorLayout;
			addChild(anchorLayoutGroup);
			
			employeeName = new Label();
			employeeName.nameList.add("employee-name-label");
			employeeName.text = employeeData.name;
			var empNameLblData:AnchorLayoutData = new AnchorLayoutData();
			empNameLblData.horizontalCenter = 0;
			empNameLblData.top = 20;
			employeeName.layoutData = empNameLblData;
			anchorLayoutGroup.addChild(employeeName);
			
			employeeTitle = new Label();
			employeeTitle.nameList.add("employee-title-label");
			employeeTitle.text = employeeData.position;
			anchorLayoutGroup.addChild(employeeTitle);
			empTitleLblData = new AnchorLayoutData();
			empTitleLblData.horizontalCenter = 0;
			employeeTitle.layoutData = empTitleLblData;
			
			employeeImage = new ImageLoader();
			employeeImage.source = File.applicationStorageDirectory.resolvePath(employeeData.imageUrl).url;
			employeeImageData = new AnchorLayoutData();
			employeeImageData.horizontalCenter = 0;
			employeeImage.layoutData = employeeImageData;
			anchorLayoutGroup.addChild(employeeImage);
			
			imageFrame = new ImageLoader();
			imageFrame.source = "assets/images/homecare/image-overlay.png";
			imageFrame.layoutData = employeeImageData;
			anchorLayoutGroup.addChild(imageFrame);
			
			starRattingComp = new StarRatting(false);
			starRatingCompData = new AnchorLayoutData();
			starRatingCompData.horizontalCenter = 0;
			starRatingCompData.bottom = 10;
			starRattingComp.layoutData = starRatingCompData;
			anchorLayoutGroup.addChild(starRattingComp);
			starRattingComp.setSelectedForNum(int(employeeData.ratingNumber));
			buttonGroup = new LayoutGroup();
			var btnLayout:VerticalLayout = new VerticalLayout();
			buttonGroup.layoutData = new VerticalLayoutData(100, NaN);
			btnLayout.gap = 15;
			btnLayout.horizontalAlign = HorizontalLayout.HORIZONTAL_ALIGN_CENTER;
			buttonGroup.layout = btnLayout;
			addChild(buttonGroup);
			
			ratingLabel = new Label();
			ratingLabel.nameList.add("employee-title-label");
			ratingLabel.text = employeeData.exprience;
			buttonGroup.addChild(ratingLabel);
			
			performanceButton = new Button();
			performanceButton.layoutData = new VerticalLayoutData(85, NaN);
			performanceButton.nameList.add("performance-btn");
			performanceButton.label = "Performance and Reviews";
			performanceButton.addEventListener(Event.TRIGGERED, onPerformnaceBtnTrigger);
			buttonGroup.addChild(performanceButton);

			super.initialize();
		}
		
		override protected function draw():void
		{
			var stageWidth:int = stage.stageWidth;
			var stageHeight:int = stage.stageHeight;
			
			anchorLayoutGroup.width = stageWidth;
			anchorLayoutGroup.height = stageHeight - (breadcrumbComp.y + breadcrumbComp.height + buttonGroup.height + 40*CommonUtils.scaleValue);
			
			employeeImageData.bottom = starRattingComp.height + 20 + starRatingCompData.bottom;
			empTitleLblData.top = employeeName.y + employeeName.height + 5;
			
			var availHeight:int = starRattingComp.y - (employeeTitle.y + employeeTitle.height) - 40;
			
			/*if(availHeight > 300)
			{
				employeeImage.width = employeeImage.height = imageFrame.width = imageFrame.height = availHeight - 100;
				starRatingCompData.bottom = 30;
			}
			
			else employeeImage.width = employeeImage.height = imageFrame.width = imageFrame.height = availHeight;*/
			employeeImage.width = employeeImage.height = availHeight;
			imageFrame.width = imageFrame.height = availHeight + 5;
			
			breadcrumbComp.width = stageWidth;
			
			super.draw();
		}
		
		private function onPerformnaceBtnTrigger(event:Event):void
		{
			this.dispatchEventWith(TRIGGER_PERFORMANCE, false, employeeData);
		}
	}
}